import axios from 'axios';
import type {
  AuthReq,
  AuthRes,
  HasAuthContentReq,
  noAuthContentReq,
  ContentRes,
  ShowRes,
  JumpContentReq,
  JumpContentRes
} from '../types/api.d';

// 创建axios实例
const axiosInstance = axios.create({
  baseURL: '',
  timeout: 15000,
  withCredentials: true
});

axiosInstance.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    return Promise.reject(error);
  }
);

const apiprefix: string = '//apigate.10jqka.com.cn/d/charge';
// 无权限接口的入参
const noAuthParams = '/advanceddiagnosis/diagnosis/v2/good_bad';
const apiOptions = {
  // 组件是否展示
  getShow: '/advanceddiagnosis/diagnosis/v2/control',
  // 判断是否有权限
  getAuth: '/smallcharge/l2/v1/auth',
  // 获取内容接口
  getHasAuthContent: noAuthParams,
  // 获取内容的原始接口
  getNoAuthContent: '/marketing/scene/json/v1/deal',
  // 获取权限位内容
  getJumpContent: '//adm.10jqka.com.cn/interface/ad/recommend'
};
Object.keys(apiOptions).forEach((key) => {
  if (!apiOptions[key].includes('//')) {
    apiOptions[key] = apiprefix + apiOptions[key];
  }
});
const getAuthApi = (params: AuthReq): Promise<AuthRes> =>
  axiosInstance.get(apiOptions.getAuth, {
    params
  });
const getShowApi = (params: {}): Promise<ShowRes> =>
  axiosInstance.get(apiOptions.getShow, { params });
const getHasAuthContentApi = (params: HasAuthContentReq): Promise<ContentRes> =>
  axiosInstance.get(apiOptions.getHasAuthContent, { params });
const getNoAuthContentApi = (params: noAuthContentReq): Promise<ContentRes> =>
  axiosInstance.get(apiOptions.getNoAuthContent, { params });
const getJumpContentApi = (params: JumpContentReq): Promise<JumpContentRes> =>
  axiosInstance.get(apiOptions.getJumpContent, { params });
export {
  getShowApi,
  getAuthApi,
  getHasAuthContentApi,
  getNoAuthContentApi,
  getJumpContentApi,
  noAuthParams
};
